package com.hxtec.polaris.service.impl;

import com.hxtec.polaris.commons.api.vo.Result;
import com.hxtec.polaris.commons.constant.Code;
import com.hxtec.polaris.commons.constant.GlobalVar;
import com.hxtec.polaris.commons.constant.Msg;
import com.hxtec.polaris.commons.dto.Page;
import com.hxtec.polaris.commons.exception.MyException;
import com.hxtec.polaris.mapper.ShopProductBaseMapper;
import com.hxtec.polaris.service.GoodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Describtion 商品相关service
 * @Author yonyong
 * @Date 2019/11/12 14:03
 * @Version 1.0.0
 **/
@Service
@Transactional
public class GoodServiceImpl implements GoodService{
    private static final String METHOD_TARGET_GROUP_NAME = "getTargrtGroupGoods";
    private static final String METHOD_GUESS_LIKE_NAME = "getGuessLikeGoods";

    @Resource
    private ShopProductBaseMapper shopProductBaseMapper;

    @Override
    public Object getAllGoods(String page, String rows,String order) {
        Integer start = getFormatInt(page,rows,true);
        Integer size = getFormatInt(page,rows,false);
        List<Map<String,Object>> mapList = shopProductBaseMapper.getAllGoods(start,size,order);
        if (null !=mapList && mapList.size()>0){
            return Result.ok(mapList);
        }else {
            //查询失败或查询到的数据为空
            throw new MyException(Result.error(Code.FAIL_4301, Msg.GET_ALL_GOODS_NULL));
        }
    }

    @Override
    public Object getGuessLikeGoods(String page, String rows,String order) {
        //如果是非法的请求
        if (!ifParamsValid(page,rows,null,order)){
            throw new MyException(Result.error(Code.FAIL_4301,Msg.PARAM_INVALID));
        }
        Integer start = getFormatInt(page,rows,true);
        Integer size = getFormatInt(page,rows,false);
        List<Map<String,Object>> mapList = shopProductBaseMapper.getGuessLikeGoods(start,size,order);
        if (null !=mapList && mapList.size()>0){
            return Result.ok(mapList);
        }else {
            //查询失败或查询到的数据为空
            throw new MyException(Result.error(Code.FAIL_4301,Msg.GET_GUESS_LIKE_GOODS_NULL));
        }
    }

    @Override
    public Object getClassifiedSelectGoods(String page, String rows) {
        Integer start = getFormatInt(page,rows,true);
        Integer size = getFormatInt(page,rows,false);
        List<Map<String,Object>> mapList = shopProductBaseMapper.getClassifiedSelectGoods(start,size);
        if (null !=mapList && mapList.size()>0){
            return Result.ok(mapList);
        }else {
            //查询失败或查询到的数据为空
            throw new MyException(Result.error(Code.FAIL_4301,Msg.GET_CLASSFIED_SELECTED_GOODS_NULL));
        }
    }

    @Override
    public Object getTargrtGroupGoods(String page, String rows,String cid,String order) {
        //如果是非法的请求
        if (!ifParamsValid(page,rows,cid,order)){
            throw new MyException(Result.error(Code.FAIL_4301,Msg.PARAM_INVALID));
        }
        Integer start = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        List<Map<String,Object>> mapList = shopProductBaseMapper.getTargrtGroupGoods(start,Integer.valueOf(rows),cid,order);
        //分页
        List<Map<String,Object>> countList = shopProductBaseMapper.getTargrtGroupGoodsCount(start,Integer.valueOf(rows),cid,order);
        Page page1 = new Page(countList.size(),Integer.valueOf(page),Integer.valueOf(rows));
        if (null !=mapList && mapList.size()>0){
            return Result.ok(page1,mapList);
        }else {
            //查询失败或查询到的数据为空
            throw new MyException(Result.error(Code.FAIL_4301,Msg.GET_GROUP_GOODS_NULL));
        }
    }

    /**
     * 返回 sql 分页 start limit
     * @param page
     * @param rows
     * @param isPage
     * @return
     */
    private Integer getFormatInt(String page,String rows,Boolean isPage){
        if (isPage){
            return StringUtils.isNumeric(page)&&StringUtils.isNumeric(rows)?
                    (Integer.valueOf(page)-1)*Integer.valueOf(rows):null;
        }else{
            return StringUtils.isNumeric(rows)?Integer.valueOf(rows):null;
        }
    }

    /**
     * 判断请求参数是否合法
     * @param page
     * @param rows
     * @return
     */
    private Boolean ifParamsValid(String page,String rows,String cid,String order){
        String shiftMethod = Thread.currentThread().getStackTrace()[2].getMethodName();
        if (!StringUtils.isNumeric(page)||!StringUtils.isNumeric(rows)){
            return false;
        }else {
            if (Integer.valueOf(page)<0 || Integer.valueOf(rows)<=0){
                return false;
            }
        }
        //如果是查询某个分类下的方法调用
        if (METHOD_TARGET_GROUP_NAME.equals(shiftMethod)){
            if (!StringUtils.isNumeric(cid)||!StringUtils.isNumeric(order)){
                return false;
            }
            if (GlobalVar.PRUDUCT_ORDER_LIST.indexOf(order)<0){
                return false;
            }
        }
        //如果是猜你喜欢下的方法调用
        if (METHOD_GUESS_LIKE_NAME.equals(shiftMethod)){
            if (!StringUtils.isNumeric(order) || GlobalVar.PRUDUCT_ORDER_LIST.indexOf(order)<0){
                return false;
            }
        }
        return true;
    }
}
